﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.ComponentModel;
using NetJsWire.Lite.Converters;
using Newtonsoft.Json;

namespace NetJsWire.Lite.Widgets.UI.Form
{
    [JsonObject( MemberSerialization = MemberSerialization.OptIn )]
    public class NumberFieldLW : TextFieldLW
    {
        public NumberFieldLW()
            : this( null )
        {
        }

        public NumberFieldLW( IContainer container )
            : base( container )
        {
            base.Xtype = "numberfield";
            AllowBlank = true;
            AllowDecimals = true;
            AllowNegative = true;
            BaseChars = "0123456789";
            DecimalPrecision = 2;
            DecimalSeparator = ".";
            MaxText = "The maximum value for this field is {maxValue}";
            MaxValue = int.MaxValue;
            MinText = "The minimum value for this field is {minValue}";
            MinValue = int.MinValue;
        }

        [Browsable( true )]
        [JsonProperty( "allowDecimals" )]
        [DefaultValue( true )]
        [Description( "False to disallow decimal values (defaults to true)" )]
        public bool AllowDecimals { get; set; }

        [Browsable( true )]
        [JsonProperty( "allowNegative" )]
        [DefaultValue( true )]
        [Description( "False to prevent entering a negative sign (defaults to true)" )]
        public bool AllowNegative { get; set; }

        [Browsable( true )]
        [JsonProperty( "autoStripChars" )]
        [DefaultValue( false )]
        [Description( "True to automatically strip not allowed characters from the field. Defaults to false" )]
        public bool AutoStripChars { get; set; }

        [Browsable( true )]
        [JsonProperty( "baseChars" )]
        [DefaultValue( "0123456789" )]
        [Description( "The base set of characters to evaluate as valid numbers (defaults to '0123456789')." )]
        public string BaseChars { get; set; }

        [Browsable( true )]
        [JsonProperty( "decimalPrecision" )]
        [DefaultValue( 2 )]
        [Description( "The maximum precision to display after the decimal separator (defaults to 2)" )]
        public int DecimalPrecision { get; set; }

        [Browsable( true )]
        [JsonProperty( "decimalSeparator" )]
        [DefaultValue( "." )]
        [Description( "Character(s) to allow as the decimal separator (defaults to '.')" )]
        public string DecimalSeparator { get; set; }

        [Browsable( true )]
        [JsonProperty( "maxText" )]
        [DefaultValue( "The maximum value for this field is {maxValue}" )]
        [Description( "Error text to display if the maximum value validation fails (defaults to 'The maximum value for this field is {maxValue}')" )]
        public string MaxText { get; set; }

        [Browsable( true )]
        [JsonProperty( "maxValue" )]
        [DefaultValue( int.MaxValue )]
        [Description( "The maximum allowed value" )]
        public int MaxValue { get; set; }

        [Browsable( true )]
        [JsonProperty( "minText" )]
        [DefaultValue( "The minimum value for this field is {minValue}" )]
        [Description( "Error text to display if the minimum value validation fails (defaults to 'The minimum value for this field is {minValue}')" )]
        public string MinText { get; set; }

        [Browsable( true )]
        [JsonProperty( "minValue" )]
        [DefaultValue( int.MinValue )]
        [Description( "The minimum allowed value" )]
        public int MinValue { get; set; }

        [Browsable( true )]
        [JsonProperty( "xtype", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Include )]
        [DefaultValue( "numberfield" )]
        [Description( "The registered xtype to create." )]
        [TypeConverter( typeof( XtypeTypeConverter ) )]
        public override string Xtype
        {
            get
            {
                return base.Xtype;
            }
            set
            {
                base.Xtype = value;
            }
        }
    }
}
